package com.qinsoft.business.recom.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;

import com.qinsoft.business.recom.bean.Recom;
import com.qinsoft.business.recom.bean.RecomItem;
import com.qinsoft.frame.messages.WriteMessage;
import com.qinsoft.frame.utils.DbUtil;


public class RecomDao
{
    public void save(Recom recom)
    {
        SqlSession sqlMap = null;
        try
        {
            Boolean flag = false;
            //获取连接对象
            sqlMap = DbUtil.getSqlMapClient();
            flag = sqlMap.selectOne("com.qinsoft.business.recom.sqlMap.recom.exist", recom.getNick());
            if(flag){
                //执行更新方法
                sqlMap.update("com.qinsoft.business.recom.sqlMap.recom.update",recom);
            }
            else{
                //执行保存方法
                sqlMap.insert("com.qinsoft.business.recom.sqlMap.recom.insert", recom);
            }
        }
        catch (Exception e)
        {
            WriteMessage.WriteErrorLog(this.getClass().toString() + ".save(" + recom.getNick() + ")", e);
        }
        finally
        {
            sqlMap.close();
        }
    }
    
    public void saveItem(Recom recom, List<RecomItem> items, String nick)
    {
        SqlSession sqlMap = null;
        try
        {
            Long recomId;
            recom.setNick(nick);
            //获取连接对象
            sqlMap = DbUtil.getSqlMapClient();
            recomId = sqlMap.selectOne("com.qinsoft.business.recom.sqlMap.recom.key", nick);
            if(recomId==null || recomId==0){
                //执行保存方法
                sqlMap.insert("com.qinsoft.business.recom.sqlMap.recom.insert", recom);
                recomId = recom.getRecomId();
            }
            else{
                //执行更新方法
                sqlMap.update("com.qinsoft.business.recom.sqlMap.recom.update",recom);
            }
            for(int i=0; items!=null&&i<items.size(); i++){
                items.get(i).setNick(nick);
                items.get(i).setRecomId(recomId);
                sqlMap.insert("com.qinsoft.business.recom.sqlMap.recom.insertItem", items.get(i));    
            }
        }
        catch (Exception e)
        {
            WriteMessage.WriteErrorLog(this.getClass().toString() + ".saveItem(" + recom.getNick() + ")", e);
        }
        finally
        {
            sqlMap.close();
        }
    }
    
    public Recom select(String nick)
    {
        SqlSession sqlMap = null;
        Recom recom = new Recom();
        try
        {
            //获取连接对象
            sqlMap = DbUtil.getSqlMapClient();
            //执行查询方法
            recom = sqlMap.selectOne("com.qinsoft.business.recom.sqlMap.recom.select",nick);
        }
        catch (Exception e)
        {
            WriteMessage.WriteErrorLog(this.getClass().toString() + ".select("+ nick +")", e);
        }
        finally
        {
            sqlMap.close();
        }
        return recom;
    }
    
    public List<RecomItem> selectItem(String nick, String type)
    {
    	SqlSession sqlMap = null;
    	List<RecomItem> list = new ArrayList<RecomItem>();
        try
        {
            //获取连接对象
            sqlMap = DbUtil.getSqlMapClient();
            Map<String,Object> map = new HashMap<String,Object>();
            map.put("nick", nick);
            map.put("type", type);
            //执行查询方法
            list = sqlMap.selectList("com.qinsoft.business.recom.sqlMap.recom.selectItem",map);
        }
        catch (Exception e)
        {
            WriteMessage.WriteErrorLog(this.getClass().toString() + ".selectItem("+ nick +")", e);
        }
        finally
        {
            sqlMap.close();
        }
        return list;
    }
    //根据条件获取相应的商品
    public List<RecomItem> selectItemByState(String nick, int state)
    {
    	SqlSession sqlMap = null;
    	List<RecomItem> list = new ArrayList<RecomItem>();
        try
        {
            //获取连接对象
            sqlMap = DbUtil.getSqlMapClient();
            Map<String,Object> map = new HashMap<String,Object>();
            map.put("nick", nick);
            map.put("state", state);
            //执行查询方法
            list = sqlMap.selectList("com.qinsoft.business.recom.sqlMap.recom.selectItemByState",map);
        }
        catch (Exception e)
        {
            WriteMessage.WriteErrorLog(this.getClass().toString() + ".selectItemByState("+ nick +","+state+")", e);
        }
        finally
        {
            sqlMap.close();
        }
        return list;
    }
    //根据条件获取多少个商品
    public List<RecomItem> selectItemByLimit(String nick, int state,int allCount)
    {
    	SqlSession sqlMap = null;
    	List<RecomItem> list = new ArrayList<RecomItem>();
        try
        {
            //获取连接对象
            sqlMap = DbUtil.getSqlMapClient();
            Map<String,Object> map = new HashMap<String,Object>();
            map.put("nick", nick);
            map.put("state", state);
            map.put("allCount", allCount);
            //执行查询方法
            list = sqlMap.selectList("com.qinsoft.business.recom.sqlMap.recom.selectItemByLimit",map);
        }
        catch (Exception e)
        {
            WriteMessage.WriteErrorLog(this.getClass().toString() + ".selectItemByState("+ nick +","+state+","+allCount+")", e);
        }
        finally
        {
            sqlMap.close();
        }
        return list;
    }
    public int selectAutoRecomTotal(Integer begin){
    	SqlSession sqlMap = null;
        int total = 0;
        try
        {
            //获取连接对象
            sqlMap = DbUtil.getSqlMapClient();
            //执行查询方法
            total = sqlMap.selectOne("com.qinsoft.business.recom.sqlMap.recom.selectAutoRecomTotal",begin);
        }
        catch (Exception e)
        {
            WriteMessage.WriteErrorLog(this.getClass().toString() + ".selectAutoRecom("+ begin +")", e);
        }
        finally
        {
            sqlMap.close();
        }
        return total;
    }
    
	public List<Recom> selectAutoRecom(Integer index,int pageSize) {
		SqlSession sqlMap = null;
        List<Recom> list = new ArrayList<Recom>();
        try
        {
            //获取连接对象
            sqlMap = DbUtil.getSqlMapClient();
            HashMap<String, Object> param = new HashMap<String, Object>();
            param.put("begin", index);
            param.put("pageSize", pageSize);
            //执行查询方法
            list = sqlMap.selectList("com.qinsoft.business.recom.sqlMap.recom.selectAutoRecom",param);
        }
        catch (Exception e)
        {
            WriteMessage.WriteErrorLog(this.getClass().toString() + ".selectAutoRecom("+ index +","+pageSize+")", e);
        }
        finally
        {
            sqlMap.close();
        }
        return list;
	}
}
